package com.ivanceras.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.ivanceras.client.view.QuestionnaireEdit;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Multiplechoice implements EntryPoint{
	VerticalPanel center = new VerticalPanel();
	VerticalPanel north = new VerticalPanel();
	VerticalPanel south = new VerticalPanel();
	VerticalPanel east = new VerticalPanel();
	VerticalPanel west = new VerticalPanel();
	HTML questionnaireHeader = new HTML();
	VerticalPanel teaserPanel = new VerticalPanel();
	public long questionnaireKey;

	VerticalPanel questionnaireListPanel = new VerticalPanel(); //questionnaire list + questionnaire creator UI
	VerticalPanel questionnairesPanel = new VerticalPanel(); //for the questionnaire list


	private final SimulationServiceAsync simulationService = GWT
			.create(SimulationService.class);

	public void onModuleLoad() {
		//Window.alert(Window.Location.getPath());
		createLayout();
		getQuestionnaires();
	}
	public void createLayout(){
		center.setWidth("500px");
		DockPanel dock = new DockPanel();
		dock.add(center,DockPanel.CENTER);
		north.setHeight("50px");
		dock.add(north,DockPanel.NORTH);
		south.setHeight("50px");
		dock.add(south,DockPanel.SOUTH);
		east.setWidth("70px");
		dock.add(east,DockPanel.EAST);
		west.setWidth("70px");
		dock.add(west,DockPanel.WEST);
		dock.setWidth("700px");
		RootPanel.get("container").add(dock);
		questionnaireListPanel.add(questionnairesPanel);
		Widget teaser = getTeaserWidget();
		teaserPanel.add(teaser);
		questionnaireListPanel.add(teaserPanel);
	}
	public void getQuestionnaires(){
		simulationService.getQuestionnaires(new AsyncCallback<ArrayList<Questionnaire>>(){

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Error"+caught.toString());
				
			}
			/**
			 * qr = Questionnaire
			 * qn = question
			 * ch = choice
			 * che = choiceEditor object
			 * qne = questionEditor object
			 * qre = questonnaireEditor object
			 * @param result
			 */

			@Override
			public void onSuccess(ArrayList<Questionnaire> qnrs) {
				displayQuestionnaires(qnrs);
			}
			
		});
	}
	public void displayQuestionnaires(ArrayList<Questionnaire> qnrs){
		for(final Questionnaire qnaire : qnrs){
			addQuestionnaireDisplay(qnaire);
		}
		HTML vspacer = new HTML();
		vspacer.setHeight("10px");
		questionnaireListPanel.add(vspacer);
		questionnaireListPanel.setWidth("100%");
		center.add(questionnaireListPanel);		
	}
	public void addQuestionnaireDisplay(final Questionnaire qnaire){
		HorizontalPanel qnaireDetailPanel = new HorizontalPanel();
		qnaireDetailPanel.setWidth("100%");
		HorizontalPanel titlePane = new HorizontalPanel();
		final HTML qnaireTitle = new HTML("<b>"+qnaire.title+"</b>");
		titlePane.add(qnaireTitle);
		qnaireTitle.setStyleName("questionnaire-list");
		HTML qnaireDesc = new HTML(qnaire.description);
		qnaireDesc.setStyleName("questionnaire-description");
		VerticalPanel qnaireDetail = new VerticalPanel();
		final Image qrImg = new Image();
		qrImg.setUrl("http://profile.ak.fbcdn.net/object2/1066/101/n252547355808_9534.jpg");
		//qrImg.setSize("100px", "100px");
		qrImg.setStyleName("questionnaire-image");
		qnaireDetailPanel.add(qrImg);
		HTML hspacer = new HTML();
		hspacer.setWidth("10px");
		qnaireDetailPanel.add(hspacer);
		qnaireDetail.add(titlePane);
		HTML totalItems = new HTML(" ("+qnaire.items+" items )");
		titlePane.add(totalItems);
		qnaireTitle.setWordWrap(false);
		totalItems.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		final EditQuestionnaireCaption editQnrCaption = new EditQuestionnaireCaption(qnaire,"edit");
		titlePane.add(editQnrCaption);
		titlePane.setCellWidth(totalItems,"98%");
		titlePane.setCellWidth(qnaireTitle, "1%");
		titlePane.setCellWidth(editQnrCaption,"1%");
		//titlePane.setBorderWidth(1);
		editQnrCaption.addMouseOverHandler(new MouseOverHandler(){

			@Override
			public void onMouseOver(MouseOverEvent event) {
				// TODO Auto-generated method stub
				editQnrCaption.setStyleName("edit-questionnaire-hover");
			}
			
		});
		editQnrCaption.addMouseOutHandler(new MouseOutHandler(){

			@Override
			public void onMouseOut(MouseOutEvent event) {
				editQnrCaption.setStyleName("edit-questionnaire");
			}
			
		});
		editQnrCaption.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				QuestionnaireEdit qnre = new QuestionnaireEdit(editQnrCaption.questionnaire);
				center.clear();
				center.add(qnre.getWidget());
			}
			
		});
		titlePane.setWidth("100%");
		qnaireDetail.setWidth("100%");
		qnaireDetail.add(qnaireDesc);
		qnaireDetailPanel.add(qnaireDetail);
		qnaireTitle.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				questionnaireListPanel.setVisible(false);
				center.add(qnaire.getWidget());
			}
		});
		qnaireTitle.addMouseOverHandler(new MouseOverHandler(){
			@Override
			public void onMouseOver(MouseOverEvent event) {
				qnaireTitle.setStyleName("questionnaire-list-hover");
			}
		});
		qnaireTitle.addMouseOutHandler(new MouseOutHandler(){
			@Override
			public void onMouseOut(MouseOutEvent event) {
				qnaireTitle.setStyleName("questionnaire-list");
			}
		});
		qrImg.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				questionnaireListPanel.setVisible(false);
				center.add(qnaire.getWidget());
			}
		});
		qrImg.addMouseOverHandler(new MouseOverHandler(){
			@Override
			public void onMouseOver(MouseOverEvent event) {
				qrImg.setStyleName("questionnaire-image-hover");
			}
		});
		qrImg.addMouseOutHandler(new MouseOutHandler(){
			@Override
			public void onMouseOut(MouseOutEvent event) {
				qrImg.setStyleName("questionnaire-image");
			}
		});
		questionnairesPanel.add(qnaireDetailPanel);
		HTML vspacer = new HTML();
		vspacer.setHeight("10px");
		vspacer.setStyleName("questionnaire-separator");
		questionnairesPanel.add(vspacer);		
	}
	public Widget getTeaserWidget(){
		final TextBox qncTeaser = new TextBox();
		qncTeaser.setText(" + Create new Questionnaire...");
		qncTeaser.setStyleName("create-question-teaser");
		qncTeaser.setHeight("50px");
		qncTeaser.setWidth("350px");
		qncTeaser.addFocusHandler(new FocusHandler(){
			@Override
			public void onFocus(FocusEvent event) {
				clearTeaserWidget();
			}
		});
		return qncTeaser;
	}
	public void clearTeaserWidget(){
		QuestionnaireEdit qnre = new QuestionnaireEdit();
		qnre.multipleChoice = Multiplechoice.this;
		teaserPanel.clear();
		teaserPanel.add(qnre.getWidget());
	}
	public void restoreTeaserBox(){
		teaserPanel.clear();
		Widget teaser = getTeaserWidget();
		teaserPanel.add(teaser);
	}

}
